/*
 *所有关于SysWorkFlowTable类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*SysWorkFlowTableService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using TradeDataAPI.Core.Extensions;
using TradeDataAPI.Core.ManageUser;
using TradeDataAPI.Core.WorkFlow;
using TradeDataAPI.Entity.DomainModels;
using TradeDataAPI.System.IRepositories;

namespace TradeDataAPI.System.Services
{
    public partial class SysWorkFlowTableService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISysWorkFlowTableRepository _repository;//访问数据库
        private readonly ISysWorkFlowTableStepRepository _stepRepository;//访问数据库
        [ActivatorUtilitiesConstructor]
        public SysWorkFlowTableService(
            ISysWorkFlowTableRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
             ISysWorkFlowTableStepRepository stepRepository
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _stepRepository = stepRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override PageGridData<SysWorkFlowTable> GetPageData(PageDataOptions options)
        {
            if (!UserContext.Current.IsSuperAdmin)
            {
                var user = UserContext.Current.UserInfo;
                //显示当前用户需要审批的数据
                var deptIds = user.DeptIds.Select(s => s.ToString());
                var stepQuery = _stepRepository.FindAsIQueryable(x => (x.StepType == (int)AuditType.用户审批 && x.StepValue == user.User_Id.ToString())
                  || (x.StepType == (int)AuditType.角色审批 && x.StepValue == user.Role_Id.ToString())
                  || (x.StepType == (int)AuditType.部门审批 && deptIds.Contains(x.StepValue))
                   );
                QueryRelativeExpression = (IQueryable<SysWorkFlowTable> queryable) =>
                {
                    return queryable.Where(x => stepQuery.Any(c => x.WorkFlowTable_Id == c.WorkFlowTable_Id));
                };
            }
            return base.GetPageData(options);
        }
    }
}
